<template>
  <div class="login-wrap"
       @keyup.enter="submitForm">
    <div class="ms-login">
      <div class="ms-title">后台管理系统</div>
      <el-form :model="param"
               :rules="rules"
               ref="login"
               label-width="0px"
               class="ms-content">
        <el-form-item prop="username">
          <el-input v-model="param.username"
                    placeholder="账号"
                    tabindex="1"
                    autofocus="true">
            <el-button slot="prepend"
                       icon="el-icon-lx-people"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password"
                    placeholder="密码"
                    v-model="param.password"
                    @keyup.enter.native="submitForm()"
                    tabindex="2">
            <el-button slot="prepend"
                       icon="el-icon-lx-lock"></el-button>
          </el-input>

        </el-form-item>
        <el-form-item prop="password">
          <el-switch v-model="param.is_remember"
                     inactive-text="记住密码">
          </el-switch>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary"
                     @keyup.enter="submitForm"
                     @click="submitForm()"
                     tabindex="3">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      param: {
        username: '',
        password: '',
        is_remember: false
      },
      is_request: false,
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    };
  },

  mounted () {
    let self = this;
    if (window.localStorage) {
      let is_remember = localStorage.getItem("is_remember");
      if (is_remember && is_remember == 1) {
        self.param.is_remember = true;
        self.param.username = localStorage.getItem("username");
        self.param.password = self.tool.encrypt.decode(localStorage.getItem("password"));
      }
    }
    //页面键盘回车响应事件
    document.onkeyup = function (ev) {
      if (ev.keyCode == 13) {
        self.submitForm();
      }
    }
  },
  methods: {
    submitForm () {
      let that = this;
      this.$refs.login.validate(valid => {
        if (!valid) {
          that.$message.error('请输入账号和密码');
          return false;
        }
        if (that.is_request) {
          return false;
        }
        that.is_request = true;
        that.api.login(this.param, function (response) {
          that.is_request = false;
          if (response.code != 1000) {
            that.$message.error(response.msg);
            return false;
          }
          if (window.localStorage) {
            if (that.param.is_remember) {
              localStorage.setItem("username", that.param.username);
              localStorage.setItem("password", that.tool.encrypt.encode(that.param.password));
              localStorage.setItem("is_remember", 1);
            } else {
              localStorage.removeItem("username");
              localStorage.removeItem("password");
              localStorage.removeItem("is_remember");
            }
          }

          that.$message.success('登录成功');
          that.$router.push('/');
        }, function () {
          that.is_request = false;
        });
      });
    },
  },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.el-switch__label {
    color: #409eff;
}
.el-switch__label.is-active {
    color: #303133;
}
</style>
